package jmine.tec.test.matchers;

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

/**
 * Matcher that captures and holds the matched argument.
 * It delegates the actual matching to another matcher.    
 * 
 * @param <T> type of the captured argument
 * @author seiti
 * Mar 19, 2012
 */
public class CapturingMatcher<T> extends BaseMatcher<T> {
    
    private final Matcher<T> delegate;

    private List<T> captured = new ArrayList<T>();

    /**
     * Ctor
     *  
     * @param delegate the matcher in effect 
     */
    public CapturingMatcher(Matcher<T> delegate){
      this.delegate = delegate;
    }

    /**
     * @return the captured argument
     */
    public List<T> getCapturedArguments(){
        return this.captured;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public boolean matches(Object arg){
        this.captured.add((T) arg);
        return this.delegate.matches(arg);
    }

    /**
     * {@inheritDoc}
     */
    public void describeTo(Description arg){
        this.delegate.describeTo(arg);
    }
}
